var this_tinyimagepaste = null;
(function () {
    tinymce.create("tinymce.plugins.tinyimagepaste", {
        ed: null,
        init: function (ed, url) {
            this.ed = ed;
            this_tinyimagepaste = this;
            setTimeout(this._hook, 1);
            ed.addButton("tinyimagepaste", {
                title: "Image Paste",
                onclick: function () {
                    alert("直接粘贴图片就ok，有问题请反馈：https://www.wandhi.com")
                },
                image: url + "/ico.png"
            })
        },
        _paste: function (content) {
            return content
        },
        _hook: function () {
            var that = this;
            jQuery("html").find("iframe").contents().find("body").ready().pasteImageReader(function (results) {
                // var apiUrl = 'https://apis.yum6.cn/api/5bd44dc94bcfc';
                var apiUrl='https://api.yum6.cn/sinaimg.php?type=multipart';
                // var token = 'f07b711396f9a05bc7129c4507fb65c5';
                debugger
                var iframe = jQuery("#poststuff").find("iframe");
                var data = new FormData();
                data.append('file', results.file);
                $.ajax({
                    url: apiUrl ,
                    type: 'POST',
                    data: data,
                    dataType: 'JSON',
                    processData: false,
                    contentType: false,
                    success: function (ret) {
                        if (ret['code'] != '200') {
                            alert("上传失败");
                            console.log(ret);
                        } else {
                            var id = iframe.contents().find("body").attr("id");
                            ed = this_tinyimagepaste.ed;
                            var src="https://ws3.sinaimg.cn/large/"+ret['pid']+".png";
                            ed.execCommand("mceInsertContent", false, this_tinyimagepaste._paste('<img src="' + src + '" />'))
                        }
                    }
                });
            })
        }
    });
    tinymce.PluginManager.add("tinyimagepaste", tinymce.plugins.tinyimagepaste)
})();